package com.song.service;

import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

@Service(value = "MaraTempService")
public class MaraTempService {

    @Transactional
    public int maraTempInsert(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        DataSource dataSource = (DataSource) context.getBean("dataSource");
        try (Connection conn = dataSource.getConnection()) {
            // 使用conn进行数据库操作
            String sql = "INSERT INTO `tn_mdm_test`.`tn_md_mara_temp` (`id`, `task_id`, `sync_flag`, `matnr_pre`, `matnr`, `mtart`, `mara_des`, `mara_name`, `matal`, `zsx1`, `zsxz1`, `zsx2`, `zsxz2`, `zsx3`, `zsxz3`, `zsx4`, `zsxz4`, `zsx5`, `zsxz5`, `zsx6`, `zsxz6`, `zsx7`, `zsxz7`, `zsx8`, `zsxz8`, `zsx9`, `zsxz9`, `zsx10`, `zsxz10`, `zsx11`, `zsxz11`, `zsx12`, `zsxz12`, `zsklx`, `zskxh`, `zmbbh`, `meins`, `spart`, `zwldl`, `zwlzl`, `zwlxl`, `zwlsl`, `zwlmsbc`, `groes`, `ferth`, `normt`, `brgew`, `ntgew`, `gewei`, `zeinr`, `extwg`, `labor`, `profl`, `prdha`, `attach_option_unit`, `attach_option_unit_numerator`, `attach_option_unit_denominator`, `attach_sale_text`, `remark`, `identify`, `coefficient_of_stack_unit`, `coefficient_of_stack_numerator`, `coefficient_of_stack_denominator`, `create_by`, `create_time`, `update_by`, `update_time`) VALUES (null, 'WT2022112100023', 'N', '3043', '', 'Z003', '标贴/陆魔C系列/6-QW-42min(245)R/38B20R/顶/无电眼', '标贴', '3002', '电池类型', '陆魔C系列', '电压容量', '6-QW-42min(245)R', '图号、编码、版本', '38B20R', '规格', '', '位置', '顶', '材质', '', '类型', '无电眼', '其他', '', '', '', '', '', '', '', '', '', '', NULL, '', 'PC', '00', '产品类', '包装类', '印刷品', '', NULL, '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '已经存在基本视图', '1', '', '', '', '', NULL, '', NULL);\n";
            PreparedStatement pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            int affectedRows = pstmt.executeUpdate();
            if (affectedRows > 0) {
                try (ResultSet rs = pstmt.getGeneratedKeys()) {
                    if (rs.next()) {
                        int id = rs.getInt(1);
                        // 使用id
                        System.out.println(id);
                    }
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new RuntimeException(throwables);
        }
        return 1;
    }

}
